package com.fenghuajueli.idiomppp.ui.fragment.shiyi;

import android.content.Context;
import android.view.View;

import com.fenghuajueli.idiomppp.database.IdiomDaoUtils;
import com.fenghuajueli.idiomppp.entity.IdiomShiYiEntity;
import com.fenghuajueli.libbasecoreui.mvp.BasePresenter;


import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class IdiomShiYiPresenter extends BasePresenter<IdiomShiYiView> {
    private List<IdiomShiYiEntity> idiomShiYiEntityList;
    private int currentPage = 0;//当前第几页
    private int pageSize = 30;//每一页的大小
    private int allCount = 0;//数据总数
    private int allPages = 0;//总的页数

    private List<IdiomShiYiEntity> allIdiomShiYiEntityList;


    public IdiomShiYiPresenter(Context mContext) {
        super(mContext);
        idiomShiYiEntityList = new ArrayList<>();
    }


    public void initData() {
        Observable.create(new ObservableOnSubscribe<List<IdiomShiYiEntity>>() {
            @Override
            public void subscribe(ObservableEmitter<List<IdiomShiYiEntity>> emitter) throws Exception {
                allIdiomShiYiEntityList = IdiomDaoUtils.getIdiomShiYiManager().listAll();
                allCount = allIdiomShiYiEntityList.size();
                emitter.onNext(allIdiomShiYiEntityList);
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<IdiomShiYiEntity>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<IdiomShiYiEntity> idiomShiYiEntityList) {
                        loadNextPageData();
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    public void loadNextPageData() {
        if (allCount % pageSize == 0) {
            allPages = allCount / pageSize;
        } else {
            allPages = allCount / pageSize + 1;
        }
        currentPage++;
        if (currentPage == allPages) {
            getView().changeBtnNextView(View.GONE);
        }
        if (currentPage > 1) {
            getView().changeBtnUpView(View.VISIBLE);
        }
        int endIndex = 0;
        if (currentPage * pageSize > allCount) {
            endIndex = allCount;
        } else {
            endIndex = currentPage * pageSize;
        }
        idiomShiYiEntityList =allIdiomShiYiEntityList.subList((currentPage - 1) * pageSize, endIndex);
        getView().loadSuccess(idiomShiYiEntityList);
    }


    public void loadLastPageData() {
        if (currentPage > 1) {
            currentPage--;
            if (currentPage == 1) {
                getView().changeBtnUpView(View.GONE);
            }
            getView().changeBtnNextView(View.VISIBLE);
            int endIndex = 0;
            if (currentPage * pageSize > allCount) {
                endIndex = allCount;
            } else {
                endIndex = currentPage * pageSize;
            }
            idiomShiYiEntityList = allIdiomShiYiEntityList.subList((currentPage - 1) * pageSize, endIndex);
        } else {
            currentPage = 1;
            idiomShiYiEntityList = allIdiomShiYiEntityList.subList((currentPage - 1) * pageSize, currentPage * pageSize);
            getView().changeBtnUpView(View.GONE);
        }
        getView().loadSuccess(idiomShiYiEntityList);
    }
}
